package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.asn1.axdr.AxdrEnum;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

import java.io.IOException;

/**
 * ProxyActionThenGetGetResult类
 * 实现代理操作后读取结果的获取结果功能
 * 
 * @author wind
 * @version 1.1.1
 */
public class ProxyActionThenGetGetResult implements AxdrType {

    private byte[] dataCode = null;
    private ProxyActionThenGetResultChoice choice = ProxyActionThenGetResultChoice._ERR_NONE_SELECTED;
    private AxdrEnum dar = null;
    private Data data = null;

    public ProxyActionThenGetGetResult() {
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setDar(AxdrEnum dar) {
        this.resetChoices();
        this.choice = ProxyActionThenGetResultChoice.DAR;
        this.dar = dar;
    }

    public void setData(Data data) {
        this.resetChoices();
        this.choice = ProxyActionThenGetResultChoice.DATA;
        this.data = data;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = ProxyActionThenGetResultChoice.fromValue((int) choosen.getValue());

        if (this.choice == ProxyActionThenGetResultChoice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.setConst();
            codeLength += this.dar.decode(input);
            return codeLength;
        } else if (this.choice == ProxyActionThenGetResultChoice.DATA) {
            this.data = new Data();
            codeLength += this.data.decode(input);
            return codeLength;
        }
        throw new RuntimeException("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        if (this.dataCode != null) {
            try {
                for (int i = this.dataCode.length - 1; i >= 0; i--) {
                    output.write(this.dataCode[i]);
                }
            } catch (IOException e) {
                throw new RuntimeException("Error writing dataCode to output stream", e);
            }
            return this.dataCode.length;
        }
        if (this.choice == ProxyActionThenGetResultChoice._ERR_NONE_SELECTED) {
            throw new RuntimeException("Error encoding AxdrChoice: No item in choice was selected.");
        }

        int codeLength = 0;

        if (this.choice == ProxyActionThenGetResultChoice.DATA) {
            if (this.data != null) {
                codeLength += this.data.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new RuntimeException("data is null");
            }
        } else if (this.choice == ProxyActionThenGetResultChoice.DAR) {
            codeLength += this.dar.encode(output);
            AxdrEnum c = new AxdrEnum();
            c.setMinMaxVal(0);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new RuntimeException("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        try {
            encode(revOStream);
        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException("Error encoding ProxyActionThenGetGetResult", e);
        }
        this.dataCode = revOStream.getArray();
    }

    public ProxyActionThenGetResultChoice getChoiceIndex() {
        return this.choice;
    }

    public AxdrEnum getDar() {
        return this.dar;
    }

    public Data getData() {
        return this.data;
    }

    public void resetChoices() {
        this.choice = ProxyActionThenGetResultChoice._ERR_NONE_SELECTED;
        this.dar = null;
        this.data = null;
    }

    @Override
    public String toString() {
        if (this.choice == ProxyActionThenGetResultChoice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        } else if (this.choice == ProxyActionThenGetResultChoice.DATA) {
            if (this.data != null) {
                return "choice: {data: " + this.data + "}";
            } else {
                return "choice is data but data is null";
            }
        } else {
            return "unknown";
        }
    }
}